{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pywinauto\n",
    "from pywinauto import clipboard\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\users\\kalen\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\pywinauto\\application.py:978: UserWarning: 32-bit application should be automated using 32-bit Python (you use 64-bit Python)\n",
      "  UserWarning)\n"
     ]
    }
   ],
   "source": [
    "app = pywinauto.Application().connect(path=r\"C:\\同花顺软件\\同花顺免费模拟炒股软件\\weituo\\模拟炒股\\xiadan.exe\",timeout=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {},
   "outputs": [],
   "source": [
    "main = app.top_window()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'网上股票交易系统5.0'"
      ]
     },
     "execution_count": 136,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "main.window_text()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "460504"
      ]
     },
     "execution_count": 137,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "main.handle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_left_menus_handle():\n",
    "    while True:\n",
    "        try:\n",
    "            handle = main.window(\n",
    "                control_id=0x81,\n",
    "#                 handle=0x20EB0,\n",
    "                class_name='SysTreeView32'\n",
    "            )\n",
    "            # sometime can't find handle ready, must retry\n",
    "            handle.wait('ready', 2)\n",
    "            return handle\n",
    "        except:\n",
    "            pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pywinauto.application.WindowSpecification at 0x29c3d5fc128>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_left_menus_handle()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 获取资金余额"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "get_left_menus_handle().get_item([4]).click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = {\n",
    "    \"资金余额\":  main.window(control_id=0x40E, class_name='Static').window_text()\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'资金余额': '49661.64'}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 查询持有股票"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [],
   "source": [
    "get_left_menus_handle().get_item([0]).click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "证券代码\t证券名称\t股票余额\t可用余额\t冻结数量\t盈亏\t成本价\t盈亏比(%)\t市价\t市值\t交易市场\t股东帐户\t实际数量\t可申赎数量\t\r\n",
      "000333\t美的集团\t200\t200\t0\t6157.880\t17.811\t172.87\t48.600\t9720.000\t深圳Ａ股\t00101106569\t200\t200\t\r\n",
      "000651\t格力电器\t1100\t1100\t0\t4060.700\t46.608\t7.92\t50.300\t55330.000\t深圳Ａ股\t00101106569\t1100\t1100\t\r\n",
      "162411\t华宝油气\t1300\t1200\t100\t-473.980\t0.591\t-61.73\t0.226\t293.800\t深圳Ａ股\t00101106569\t1300\t1200\t\r\n",
      "600048\t保利地产\t3800\t3800\t0\t2718.060\t13.285\t5.38\t14.000\t53200.000\t上海Ａ股\tA476826692\t3800\t3800\t\r\n",
      "601155\t新城控股\t1600\t1600\t0\t-3270.520\t29.284\t-6.98\t27.240\t43584.000\t上海Ａ股\tA476826692\t1600\t1600\n"
     ]
    }
   ],
   "source": [
    "switch_tab = main.window(\n",
    "    control_id=0x54B,\n",
    "    class_name='CCustomTabCtrl'\n",
    ")\n",
    "time.sleep(0.5)\n",
    "main.set_focus()\n",
    "time.sleep(0.5)\n",
    "pywinauto.keyboard.SendKeys('^w')\n",
    "\n",
    "grid = main.window(control_id=0x417, class_name='CVirtualGridCtrl')\n",
    "\n",
    "grid.set_focus() #.right_click()   # 模拟右键\n",
    "\n",
    "pywinauto.keyboard.SendKeys('^c')\n",
    "# time.sleep(0.1)\n",
    "# pywinauto.keyboard.SendKeys('{DOWN}')\n",
    "# time.sleep(0.1)\n",
    "# pywinauto.keyboard.SendKeys(\"{ENTER}\")\n",
    "\n",
    "\n",
    "print(clipboard.GetData())  # "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import io"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>证券代码</th>\n",
       "      <th>证券名称</th>\n",
       "      <th>股票余额</th>\n",
       "      <th>可用余额</th>\n",
       "      <th>冻结数量</th>\n",
       "      <th>盈亏</th>\n",
       "      <th>成本价</th>\n",
       "      <th>盈亏比(%)</th>\n",
       "      <th>市价</th>\n",
       "      <th>市值</th>\n",
       "      <th>交易市场</th>\n",
       "      <th>股东帐户</th>\n",
       "      <th>实际数量</th>\n",
       "      <th>可申赎数量</th>\n",
       "      <th>Unnamed: 14</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>333</td>\n",
       "      <td>美的集团</td>\n",
       "      <td>200</td>\n",
       "      <td>200</td>\n",
       "      <td>0</td>\n",
       "      <td>6157.88</td>\n",
       "      <td>17.811</td>\n",
       "      <td>172.87</td>\n",
       "      <td>48.600</td>\n",
       "      <td>9720.0</td>\n",
       "      <td>深圳Ａ股</td>\n",
       "      <td>00101106569</td>\n",
       "      <td>200</td>\n",
       "      <td>200</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>651</td>\n",
       "      <td>格力电器</td>\n",
       "      <td>1100</td>\n",
       "      <td>1100</td>\n",
       "      <td>0</td>\n",
       "      <td>4060.70</td>\n",
       "      <td>46.608</td>\n",
       "      <td>7.92</td>\n",
       "      <td>50.300</td>\n",
       "      <td>55330.0</td>\n",
       "      <td>深圳Ａ股</td>\n",
       "      <td>00101106569</td>\n",
       "      <td>1100</td>\n",
       "      <td>1100</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>162411</td>\n",
       "      <td>华宝油气</td>\n",
       "      <td>1300</td>\n",
       "      <td>1200</td>\n",
       "      <td>100</td>\n",
       "      <td>-473.98</td>\n",
       "      <td>0.591</td>\n",
       "      <td>-61.73</td>\n",
       "      <td>0.226</td>\n",
       "      <td>293.8</td>\n",
       "      <td>深圳Ａ股</td>\n",
       "      <td>00101106569</td>\n",
       "      <td>1300</td>\n",
       "      <td>1200</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>600048</td>\n",
       "      <td>保利地产</td>\n",
       "      <td>3800</td>\n",
       "      <td>3800</td>\n",
       "      <td>0</td>\n",
       "      <td>2718.06</td>\n",
       "      <td>13.285</td>\n",
       "      <td>5.38</td>\n",
       "      <td>14.000</td>\n",
       "      <td>53200.0</td>\n",
       "      <td>上海Ａ股</td>\n",
       "      <td>A476826692</td>\n",
       "      <td>3800</td>\n",
       "      <td>3800</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>601155</td>\n",
       "      <td>新城控股</td>\n",
       "      <td>1600</td>\n",
       "      <td>1600</td>\n",
       "      <td>0</td>\n",
       "      <td>-3270.52</td>\n",
       "      <td>29.284</td>\n",
       "      <td>-6.98</td>\n",
       "      <td>27.240</td>\n",
       "      <td>43584.0</td>\n",
       "      <td>上海Ａ股</td>\n",
       "      <td>A476826692</td>\n",
       "      <td>1600</td>\n",
       "      <td>1600</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     证券代码  证券名称  股票余额  可用余额  冻结数量       盈亏     成本价  盈亏比(%)      市价       市值  \\\n",
       "0     333  美的集团   200   200     0  6157.88  17.811  172.87  48.600   9720.0   \n",
       "1     651  格力电器  1100  1100     0  4060.70  46.608    7.92  50.300  55330.0   \n",
       "2  162411  华宝油气  1300  1200   100  -473.98   0.591  -61.73   0.226    293.8   \n",
       "3  600048  保利地产  3800  3800     0  2718.06  13.285    5.38  14.000  53200.0   \n",
       "4  601155  新城控股  1600  1600     0 -3270.52  29.284   -6.98  27.240  43584.0   \n",
       "\n",
       "   交易市场         股东帐户  实际数量  可申赎数量 Unnamed: 14  \n",
       "0  深圳Ａ股  00101106569   200    200              \n",
       "1  深圳Ａ股  00101106569  1100   1100              \n",
       "2  深圳Ａ股  00101106569  1300   1200              \n",
       "3  上海Ａ股   A476826692  3800   3800              \n",
       "4  上海Ａ股   A476826692  1600   1600              "
      ]
     },
     "execution_count": 142,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = clipboard.GetData()\n",
    "df = pd.read_csv(io.StringIO(data),\n",
    "                 delimiter='\\t',\n",
    "                 na_filter=False)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'证券代码': 333,\n",
       "  '证券名称': '美的集团',\n",
       "  '股票余额': 200,\n",
       "  '可用余额': 200,\n",
       "  '冻结数量': 0,\n",
       "  '盈亏': 6157.88,\n",
       "  '成本价': 17.811,\n",
       "  '盈亏比(%)': 172.87,\n",
       "  '市价': 48.6,\n",
       "  '市值': 9720.0,\n",
       "  '交易市场': '深圳Ａ股',\n",
       "  '股东帐户': '00101106569',\n",
       "  '实际数量': 200,\n",
       "  '可申赎数量': 200,\n",
       "  'Unnamed: 14': ''},\n",
       " {'证券代码': 651,\n",
       "  '证券名称': '格力电器',\n",
       "  '股票余额': 1100,\n",
       "  '可用余额': 1100,\n",
       "  '冻结数量': 0,\n",
       "  '盈亏': 4060.7,\n",
       "  '成本价': 46.608000000000004,\n",
       "  '盈亏比(%)': 7.92,\n",
       "  '市价': 50.3,\n",
       "  '市值': 55330.0,\n",
       "  '交易市场': '深圳Ａ股',\n",
       "  '股东帐户': '00101106569',\n",
       "  '实际数量': 1100,\n",
       "  '可申赎数量': 1100,\n",
       "  'Unnamed: 14': ''},\n",
       " {'证券代码': 162411,\n",
       "  '证券名称': '华宝油气',\n",
       "  '股票余额': 1300,\n",
       "  '可用余额': 1200,\n",
       "  '冻结数量': 100,\n",
       "  '盈亏': -473.98,\n",
       "  '成本价': 0.591,\n",
       "  '盈亏比(%)': -61.73,\n",
       "  '市价': 0.226,\n",
       "  '市值': 293.8,\n",
       "  '交易市场': '深圳Ａ股',\n",
       "  '股东帐户': '00101106569',\n",
       "  '实际数量': 1300,\n",
       "  '可申赎数量': 1200,\n",
       "  'Unnamed: 14': ''},\n",
       " {'证券代码': 600048,\n",
       "  '证券名称': '保利地产',\n",
       "  '股票余额': 3800,\n",
       "  '可用余额': 3800,\n",
       "  '冻结数量': 0,\n",
       "  '盈亏': 2718.06,\n",
       "  '成本价': 13.285,\n",
       "  '盈亏比(%)': 5.38,\n",
       "  '市价': 14.0,\n",
       "  '市值': 53200.0,\n",
       "  '交易市场': '上海Ａ股',\n",
       "  '股东帐户': 'A476826692',\n",
       "  '实际数量': 3800,\n",
       "  '可申赎数量': 3800,\n",
       "  'Unnamed: 14': ''},\n",
       " {'证券代码': 601155,\n",
       "  '证券名称': '新城控股',\n",
       "  '股票余额': 1600,\n",
       "  '可用余额': 1600,\n",
       "  '冻结数量': 0,\n",
       "  '盈亏': -3270.52,\n",
       "  '成本价': 29.284000000000002,\n",
       "  '盈亏比(%)': -6.98,\n",
       "  '市价': 27.24,\n",
       "  '市值': 43584.0,\n",
       "  '交易市场': '上海Ａ股',\n",
       "  '股东帐户': 'A476826692',\n",
       "  '实际数量': 1600,\n",
       "  '可申赎数量': 1600,\n",
       "  'Unnamed: 14': ''}]"
      ]
     },
     "execution_count": 143,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.to_dict('records')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "162127.8"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"市值\"].sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 模拟买入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "get_left_menus_handle().get_item(['买入[F1]']).click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "grid = main.window(control_id=0x417, class_name='CVirtualGridCtrl')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "stock_id = \"162411\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "您的买入委托已成功提交，合同编号：1428398429。\n"
     ]
    }
   ],
   "source": [
    "main.window(control_id=0x408, class_name=\"Edit\").set_text(stock_id)  # 设置股票代码\n",
    "\n",
    "main.window(control_id=0x409, class_name=\"Edit\").set_text(\"0.218\")  # 设置价格\n",
    "\n",
    "main.window(control_id=0x40A, class_name=\"Edit\").set_text(\"100\")   # 设置股数目\n",
    "\n",
    "main.window(control_id=0x3EE, class_name=\"Button\").click()   # 点击买入\n",
    "time.sleep(0.5)\n",
    "app.top_window().set_focus()\n",
    "app.top_window().window(control_id=0x6, class_name='Button').click()  # 确定买入\n",
    "time.sleep(0.5)\n",
    "app.top_window().set_focus()\n",
    "print(app.top_window().window(control_id=0x3EC, class_name='Static').window_text())\n",
    "time.sleep(0.5)\n",
    "app.top_window().set_focus()\n",
    "app.top_window().window(control_id=0x2, class_name='Button').click()  # 确定"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 模拟卖出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "get_left_menus_handle().get_item(['卖出[F2]']).click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "您的卖出委托已成功提交，合同编号：1428473361。\n"
     ]
    }
   ],
   "source": [
    "main.window(control_id=0x408, class_name=\"Edit\").set_text(stock_id)  # 设置股票代码\n",
    "\n",
    "main.window(control_id=0x409, class_name=\"Edit\").set_text(\"0.238\")  # 设置价格\n",
    "\n",
    "main.window(control_id=0x40A, class_name=\"Edit\").set_text(\"100\")   # 设置股数目\n",
    "\n",
    "main.window(control_id=0x3EE, class_name=\"Button\").click()   # 点击卖出\n",
    "time.sleep(0.5)\n",
    "app.top_window().set_focus()\n",
    "app.top_window().window(control_id=0x6, class_name='Button').click()  # 确定卖出\n",
    "time.sleep(0.5)\n",
    "app.top_window().set_focus()\n",
    "print(app.top_window().window(control_id=0x3EC, class_name='Static').window_text())\n",
    "time.sleep(0.5)\n",
    "app.top_window().set_focus()\n",
    "app.top_window().window(control_id=0x2, class_name='Button').click()  # 确定"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 获取当前的委托"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "get_left_menus_handle().get_item([0]).click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "委托时间\t证券代码\t证券名称\t操作\t备注\t委托数量\t成交数量\t撤消数量\t委托价格\t订单类型\t成交均价\t合同编号\t\r\n",
      "16:53:01\t162411\t华宝油气\t买入\t全部撤单\t100\t0\t100\t0.218\t限价\t0.000\t1428348934\t\r\n",
      "16:54:30\t162411\t华宝油气\t买入\t全部撤单\t100\t0\t100\t0.218\t限价\t0.000\t1428398429\t\r\n",
      "17:00:25\t162411\t华宝油气\t卖出\t未成交\t100\t0\t0\t0.238\t限价\t0.000\t1428473361\t\r\n",
      "17:06:34\t162411\t华宝油气\t买入\t未成交\t100\t0\t0\t0.226\t限价\t0.000\t1428426470\t\r\n",
      "16:52:04\t162411\t华宝油气\t买入\t全部撤单\t100\t0\t100\t0.218\t限价\t0.000\t1428339249\n"
     ]
    }
   ],
   "source": [
    "switch_tab = main.window(\n",
    "    control_id=0x54B,\n",
    "    class_name='CCustomTabCtrl'\n",
    ")\n",
    "main.set_focus()\n",
    "time.sleep(0.5)\n",
    "pywinauto.keyboard.SendKeys('^r')\n",
    "\n",
    "grid = main.window(control_id=0x417, class_name='CVirtualGridCtrl')\n",
    "\n",
    "grid.set_focus() #.right_click()   # 模拟右键\n",
    "\n",
    "pywinauto.keyboard.SendKeys('^c')\n",
    "\n",
    "data = clipboard.GetData() #\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>委托时间</th>\n",
       "      <th>证券代码</th>\n",
       "      <th>证券名称</th>\n",
       "      <th>操作</th>\n",
       "      <th>备注</th>\n",
       "      <th>委托数量</th>\n",
       "      <th>成交数量</th>\n",
       "      <th>撤消数量</th>\n",
       "      <th>委托价格</th>\n",
       "      <th>订单类型</th>\n",
       "      <th>成交均价</th>\n",
       "      <th>合同编号</th>\n",
       "      <th>Unnamed: 12</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>16:53:01</td>\n",
       "      <td>162411</td>\n",
       "      <td>华宝油气</td>\n",
       "      <td>买入</td>\n",
       "      <td>全部撤单</td>\n",
       "      <td>100</td>\n",
       "      <td>0</td>\n",
       "      <td>100</td>\n",
       "      <td>0.218</td>\n",
       "      <td>限价</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1428348934</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>16:54:30</td>\n",
       "      <td>162411</td>\n",
       "      <td>华宝油气</td>\n",
       "      <td>买入</td>\n",
       "      <td>全部撤单</td>\n",
       "      <td>100</td>\n",
       "      <td>0</td>\n",
       "      <td>100</td>\n",
       "      <td>0.218</td>\n",
       "      <td>限价</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1428398429</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>17:00:25</td>\n",
       "      <td>162411</td>\n",
       "      <td>华宝油气</td>\n",
       "      <td>卖出</td>\n",
       "      <td>未成交</td>\n",
       "      <td>100</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.238</td>\n",
       "      <td>限价</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1428473361</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>17:06:34</td>\n",
       "      <td>162411</td>\n",
       "      <td>华宝油气</td>\n",
       "      <td>买入</td>\n",
       "      <td>未成交</td>\n",
       "      <td>100</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.226</td>\n",
       "      <td>限价</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1428426470</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>16:52:04</td>\n",
       "      <td>162411</td>\n",
       "      <td>华宝油气</td>\n",
       "      <td>买入</td>\n",
       "      <td>全部撤单</td>\n",
       "      <td>100</td>\n",
       "      <td>0</td>\n",
       "      <td>100</td>\n",
       "      <td>0.218</td>\n",
       "      <td>限价</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1428339249</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       委托时间    证券代码  证券名称  操作    备注  委托数量  成交数量  撤消数量   委托价格 订单类型  成交均价  \\\n",
       "0  16:53:01  162411  华宝油气  买入  全部撤单   100     0   100  0.218   限价   0.0   \n",
       "1  16:54:30  162411  华宝油气  买入  全部撤单   100     0   100  0.218   限价   0.0   \n",
       "2  17:00:25  162411  华宝油气  卖出   未成交   100     0     0  0.238   限价   0.0   \n",
       "3  17:06:34  162411  华宝油气  买入   未成交   100     0     0  0.226   限价   0.0   \n",
       "4  16:52:04  162411  华宝油气  买入  全部撤单   100     0   100  0.218   限价   0.0   \n",
       "\n",
       "         合同编号 Unnamed: 12  \n",
       "0  1428348934              \n",
       "1  1428398429              \n",
       "2  1428473361              \n",
       "3  1428426470              \n",
       "4  1428339249              "
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv(io.StringIO(data),\n",
    "                 delimiter='\\t',\n",
    "                 na_filter=False)\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 获取当日成交"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [],
   "source": [
    "get_left_menus_handle().get_item([0]).click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "成交时间\t证券代码\t证券名称\t操作\t成交数量\t成交均价\t成交金额\t合同编号\t成交编号\t撤单数量\t股票余额\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>成交时间</th>\n",
       "      <th>证券代码</th>\n",
       "      <th>证券名称</th>\n",
       "      <th>操作</th>\n",
       "      <th>成交数量</th>\n",
       "      <th>成交均价</th>\n",
       "      <th>成交金额</th>\n",
       "      <th>合同编号</th>\n",
       "      <th>成交编号</th>\n",
       "      <th>撤单数量</th>\n",
       "      <th>股票余额</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [成交时间, 证券代码, 证券名称, 操作, 成交数量, 成交均价, 成交金额, 合同编号, 成交编号, 撤单数量, 股票余额]\n",
       "Index: []"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "switch_tab = main.window(\n",
    "    control_id=0x54B,\n",
    "    class_name='CCustomTabCtrl'\n",
    ")\n",
    "main.set_focus()\n",
    "time.sleep(0.5)\n",
    "pywinauto.keyboard.SendKeys('^e')\n",
    "\n",
    "grid = main.window(control_id=0x417, class_name='CVirtualGridCtrl')\n",
    "\n",
    "grid.set_focus() #.right_click()   # 模拟右键\n",
    "\n",
    "pywinauto.keyboard.SendKeys('^c')\n",
    "\n",
    "data = clipboard.GetData() #\n",
    "print(data)\n",
    "df = pd.read_csv(io.StringIO(data),\n",
    "                 delimiter='\\t',\n",
    "                 na_filter=False)\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 撤单"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [],
   "source": [
    "get_left_menus_handle().get_item([0]).click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [],
   "source": [
    "switch_tab = main.window(\n",
    "    control_id=0x54B,\n",
    "    class_name='CCustomTabCtrl'\n",
    ")\n",
    "time.sleep(0.5)\n",
    "main.set_focus()\n",
    "time.sleep(0.5)\n",
    "pywinauto.keyboard.SendKeys('^r')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "委托时间\t证券代码\t证券名称\t操作\t备注\t委托数量\t成交数量\t撤消数量\t委托价格\t订单类型\t成交均价\t合同编号\t\r\n",
      "16:53:01\t162411\t华宝油气\t买入\t全部撤单\t100\t0\t100\t0.218\t限价\t0.000\t1428348934\t\r\n",
      "16:54:30\t162411\t华宝油气\t买入\t全部撤单\t100\t0\t100\t0.218\t限价\t0.000\t1428398429\t\r\n",
      "17:00:25\t162411\t华宝油气\t卖出\t未成交\t100\t0\t0\t0.238\t限价\t0.000\t1428473361\t\r\n",
      "17:06:34\t162411\t华宝油气\t买入\t未成交\t100\t0\t0\t0.226\t限价\t0.000\t1428426470\t\r\n",
      "17:27:00\t162411\t华宝油气\t买入\t未成交\t100\t0\t0\t0.226\t限价\t0.000\t1428521310\t\r\n",
      "17:27:15\t162411\t华宝油气\t买入\t未成交\t100\t0\t0\t0.226\t限价\t0.000\t1428534004\t\r\n",
      "16:52:04\t162411\t华宝油气\t买入\t全部撤单\t100\t0\t100\t0.218\t限价\t0.000\t1428339249\n"
     ]
    }
   ],
   "source": [
    "def get_entrusts():\n",
    "    grid = main.window(\n",
    "        control_id=0x417,\n",
    "        class_name='CVirtualGridCtrl'\n",
    "    )\n",
    "    grid.set_focus()\n",
    "    time.sleep(0.5)\n",
    "    pywinauto.keyboard.SendKeys('^c')\n",
    "    time.sleep(0.5)\n",
    "\n",
    "    data = clipboard.GetData()\n",
    "    print(data)\n",
    "    df = pd.read_csv(io.StringIO(data),\n",
    "                     delimiter='\\t',\n",
    "                     na_filter=False)\n",
    "    return df.to_dict('records')\n",
    "\n",
    "entrusts = get_entrusts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'委托时间': '16:53:01',\n",
       "  '证券代码': 162411,\n",
       "  '证券名称': '华宝油气',\n",
       "  '操作': '买入',\n",
       "  '备注': '全部撤单',\n",
       "  '委托数量': 100,\n",
       "  '成交数量': 0,\n",
       "  '撤消数量': 100,\n",
       "  '委托价格': 0.218,\n",
       "  '订单类型': '限价',\n",
       "  '成交均价': 0.0,\n",
       "  '合同编号': 1428348934,\n",
       "  'Unnamed: 12': ''},\n",
       " {'委托时间': '16:54:30',\n",
       "  '证券代码': 162411,\n",
       "  '证券名称': '华宝油气',\n",
       "  '操作': '买入',\n",
       "  '备注': '全部撤单',\n",
       "  '委托数量': 100,\n",
       "  '成交数量': 0,\n",
       "  '撤消数量': 100,\n",
       "  '委托价格': 0.218,\n",
       "  '订单类型': '限价',\n",
       "  '成交均价': 0.0,\n",
       "  '合同编号': 1428398429,\n",
       "  'Unnamed: 12': ''},\n",
       " {'委托时间': '17:00:25',\n",
       "  '证券代码': 162411,\n",
       "  '证券名称': '华宝油气',\n",
       "  '操作': '卖出',\n",
       "  '备注': '未成交',\n",
       "  '委托数量': 100,\n",
       "  '成交数量': 0,\n",
       "  '撤消数量': 0,\n",
       "  '委托价格': 0.23800000000000002,\n",
       "  '订单类型': '限价',\n",
       "  '成交均价': 0.0,\n",
       "  '合同编号': 1428473361,\n",
       "  'Unnamed: 12': ''},\n",
       " {'委托时间': '17:06:34',\n",
       "  '证券代码': 162411,\n",
       "  '证券名称': '华宝油气',\n",
       "  '操作': '买入',\n",
       "  '备注': '未成交',\n",
       "  '委托数量': 100,\n",
       "  '成交数量': 0,\n",
       "  '撤消数量': 0,\n",
       "  '委托价格': 0.226,\n",
       "  '订单类型': '限价',\n",
       "  '成交均价': 0.0,\n",
       "  '合同编号': 1428426470,\n",
       "  'Unnamed: 12': ''},\n",
       " {'委托时间': '17:27:00',\n",
       "  '证券代码': 162411,\n",
       "  '证券名称': '华宝油气',\n",
       "  '操作': '买入',\n",
       "  '备注': '未成交',\n",
       "  '委托数量': 100,\n",
       "  '成交数量': 0,\n",
       "  '撤消数量': 0,\n",
       "  '委托价格': 0.226,\n",
       "  '订单类型': '限价',\n",
       "  '成交均价': 0.0,\n",
       "  '合同编号': 1428521310,\n",
       "  'Unnamed: 12': ''},\n",
       " {'委托时间': '17:27:15',\n",
       "  '证券代码': 162411,\n",
       "  '证券名称': '华宝油气',\n",
       "  '操作': '买入',\n",
       "  '备注': '未成交',\n",
       "  '委托数量': 100,\n",
       "  '成交数量': 0,\n",
       "  '撤消数量': 0,\n",
       "  '委托价格': 0.226,\n",
       "  '订单类型': '限价',\n",
       "  '成交均价': 0.0,\n",
       "  '合同编号': 1428534004,\n",
       "  'Unnamed: 12': ''},\n",
       " {'委托时间': '16:52:04',\n",
       "  '证券代码': 162411,\n",
       "  '证券名称': '华宝油气',\n",
       "  '操作': '买入',\n",
       "  '备注': '全部撤单',\n",
       "  '委托数量': 100,\n",
       "  '成交数量': 0,\n",
       "  '撤消数量': 100,\n",
       "  '委托价格': 0.218,\n",
       "  '订单类型': '限价',\n",
       "  '成交均价': 0.0,\n",
       "  '合同编号': 1428339249,\n",
       "  'Unnamed: 12': ''}]"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "entrusts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [],
   "source": [
    "entrust_no = \"1428473361\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cancel_by_double_click(row):\n",
    "    app.top_window().set_focus()\n",
    "    x = 50\n",
    "    y = 50 + 20 * row\n",
    "    app.top_window().window(\n",
    "            control_id=0x417,\n",
    "            class_name='CVirtualGridCtrl'\n",
    "    ).double_click(coords=(x, y))\n",
    "    \n",
    "#     app.top_window().window(control_id=0x6, class_name='Button').click()  # 确定撤单"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1428348934\n",
      "canceling 1428473361\n",
      "1 1428398429\n",
      "2 1428473361\n",
      "3 1428426470\n",
      "4 1428521310\n",
      "5 1428534004\n",
      "6 1428339249\n"
     ]
    }
   ],
   "source": [
    "for i, entrust in enumerate(entrusts):\n",
    "    print(i, entrust[\"合同编号\"])\n",
    "#     if str(entrust[\"合同编号\"]) == entrust_no:\n",
    "    if i == 0:\n",
    "        print(\"canceling\", entrust_no)\n",
    "        cancel_by_double_click(i)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
